package es.unileon.aic.floatingpointalu.userInterface;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import es.unileon.aic.floatingpointalu.core.Calculator;

/**
 * Interfaz de usuario del programa
 * @author Abel Mayorga Gonz&aacute;lez
 * @author Beatriz Justel Garc&iacute;a
 * @version 1.0
 *
 */

public class UserInterface extends JFrame{
	
	/** Botones de selecci&oacute;n de operaci&oacute;n **/
	private ImageRadioButton _plusButton, _minusButton,  _timesButton, _dividedButton;
	/** Campos de texto para los operandos **/
	private JTextFieldFloat _floatField1, _floatField2;
	/** Campos de texto para mostrar los operandos en hexadecimal, as&iacute; como el resultado **/
	private JTextField _hexaOp1, _hexaOp2, _hexaRes, _textFieldRes;
	
	/** Mensaje que se muestra cuando no se han introducido dos operandos **/
	private String _noNumbersMessage = "Tienes que introducir antes los dos operandos";
	/** Mensaje que se muestra cuando no se ha seleccionado una operaci&oacute;n **/
	private String _noOperationMessage = "Tienes que seleccionar la operaci" + 
			Character.toString('\u00F3') + "n que quieres realizar";
	/** T&iacute;tulo de la ventana principal **/
	private String _title = "Pr" + Character.toString('\u00E1') + "ctica n" + Character.toString('\u00FA') + 
			"mero 2: Floating Point ALU";
	
	/**
	 * Constructor
	 */
	public UserInterface(){
		setTitle(_title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600,400);
		setResizable(false);
		initComponent();
	}

	/**
	 * Inicializa los componentes
	 */
	private void initComponent(){
		Color fondoBotones = new Color(0x4F81BD);
		Color fondoAlu = new Color(0xDBE8FF);
		
		_plusButton = new ImageRadioButton(new ImageIcon(getClass().getResource("/icon/plus.png")));
		_plusButton.setRolloverIcon(new ImageIcon(getClass().getResource("/icon/plus-over.png")));
		_plusButton.setPressedIcon(new ImageIcon(getClass().getResource("/icon/plus-down.png")));
		_plusButton.setSelectedIcon(new ImageIcon(getClass().getResource("/icon/plus-sel.png")));
		_plusButton.setRolloverSelectedIcon(new ImageIcon(getClass().getResource("/icon/plus-down.png")));
		
		_minusButton = new ImageRadioButton(new ImageIcon(getClass().getResource("/icon/minus.png")));
		_minusButton.setRolloverIcon(new ImageIcon(getClass().getResource("/icon/minus-over.png")));
		_minusButton.setPressedIcon(new ImageIcon(getClass().getResource("/icon/minus-down.png")));
		_minusButton.setSelectedIcon(new ImageIcon(getClass().getResource("/icon/minus-sel.png")));
		_minusButton.setRolloverSelectedIcon(new ImageIcon(getClass().getResource("/icon/minus-down.png")));
		
		_timesButton = new ImageRadioButton(new ImageIcon(getClass().getResource("/icon/times.png")));
		_timesButton.setRolloverIcon(new ImageIcon(getClass().getResource("/icon/times-over.png")));
		_timesButton.setPressedIcon(new ImageIcon(getClass().getResource("/icon/times-down.png")));
		_timesButton.setSelectedIcon(new ImageIcon(getClass().getResource("/icon/times-sel.png")));
		_timesButton.setRolloverSelectedIcon(new ImageIcon(getClass().getResource("/icon/times-down.png")));
		
		_dividedButton = new ImageRadioButton(new ImageIcon(getClass().getResource("/icon/divided.png")));
		_dividedButton.setRolloverIcon(new ImageIcon(getClass().getResource("/icon/divided-over.png")));
		_dividedButton.setPressedIcon(new ImageIcon(getClass().getResource("/icon/divided-down.png")));
		_dividedButton.setSelectedIcon(new ImageIcon(getClass().getResource("/icon/divided-sel.png")));
		_dividedButton.setRolloverSelectedIcon(new ImageIcon(getClass().getResource("/icon/divided-down.png")));
	
		_plusButton.setContentAreaFilled(false);
		_minusButton.setContentAreaFilled(false);	
		_timesButton.setContentAreaFilled(false);
		_dividedButton.setContentAreaFilled(false);
		
		ButtonGroup botones = new ButtonGroup();
		botones.add(_plusButton);
		botones.add(_minusButton);
		botones.add(_timesButton);
		botones.add(_dividedButton);
		
		JPanel panelBotones = new JPanel(new GridLayout(4,1));
		panelBotones.add(_plusButton);
		panelBotones.add(_minusButton);
		panelBotones.add(_timesButton);
		panelBotones.add(_dividedButton);
		getContentPane().add(panelBotones,BorderLayout.LINE_END);
		panelBotones.setBackground(fondoBotones);
		
		PanelLines panelAlu = new PanelLines();
		panelAlu.setLayout(null);
		panelAlu.setPreferredSize(new Dimension(500,400));
		panelAlu.setBackground(fondoAlu);
		getContentPane().add(panelAlu);
		
		ImageLabel alu = new ImageLabel(new ImageIcon(getClass().getResource("/icon/alu.png")));
		panelAlu.add(alu);
		alu.setLocation(197,50);
		
		_floatField1 = new JTextFieldFloat();
		panelAlu.add(_floatField1);
		_floatField1.setBounds(20,60,100,40);
	
		_floatField1.addKeyListener(new KeyAdapter(){
			@Override
			public void keyReleased(KeyEvent arg0) {
				float numero;
				try{
					numero = Float.parseFloat(_floatField1.getText());
				} catch (NumberFormatException e){
					numero = 0.0f;
				}
				_hexaOp1.setText("0x" + Integer.toHexString(Float.floatToRawIntBits(numero)));	
			}
			
		});
		
		_hexaOp1 = new JTextField();
		panelAlu.add(_hexaOp1);
		_hexaOp1.setBounds(20, 140, 100, 40);
		_hexaOp1.setEditable(false);
		_hexaOp1.setHorizontalAlignment(SwingConstants.RIGHT);
		
		_floatField2 = new JTextFieldFloat();
		panelAlu.add(_floatField2);
		_floatField2.setBounds(20,218,100,40);
		
		_floatField2.addKeyListener(new KeyAdapter(){
			@Override
			public void keyReleased(KeyEvent arg0) {
				float numero;
				try{
					numero = Float.parseFloat(_floatField2.getText());
				} catch (NumberFormatException e){
					numero = 0.0f;
				}
				_hexaOp2.setText("0x" + Integer.toHexString(Float.floatToRawIntBits(numero)));	
			}
			
		});
		
		_hexaOp2 = new JTextField();
		panelAlu.add(_hexaOp2);
		_hexaOp2.setBounds(20,298,100,40);
		_hexaOp2.setEditable(false);
		_hexaOp2.setHorizontalAlignment(SwingConstants.RIGHT);
		
		_textFieldRes = new JTextField();
		panelAlu.add(_textFieldRes);
		_textFieldRes.setBounds(380, 140, 100, 40);
		_textFieldRes.setEditable(false);
		_textFieldRes.setHorizontalAlignment(SwingConstants.RIGHT);
		
		_hexaRes = new JTextField();
		panelAlu.add(_hexaRes);
		_hexaRes.setBounds(380,220,100,40);
		_hexaRes.setEditable(false);
		_hexaRes.setHorizontalAlignment(SwingConstants.RIGHT);
		
		panelAlu.addLine(new Point(120, 80), new Point(210, 80));
		panelAlu.addLine(new Point(120, 238), new Point(210, 238));
		panelAlu.addLine(new Point(295, 160), new Point(380, 160));
		panelAlu.addLine(new Point(70,95), new Point(70,145));
		panelAlu.addLine(new Point(70,253), new Point(70,303));
		panelAlu.addLine(new Point(430,175), new Point(430,225));
		
		ImageButton goButton = new ImageButton(new ImageIcon(getClass().getResource("/icon/action.png")));
		goButton.setRolloverIcon(new ImageIcon(getClass().getResource("/icon/action-over.png")));
		goButton.setPressedIcon(new ImageIcon(getClass().getResource("/icon/action-down.png")));
		goButton.setDisabledIcon(new ImageIcon(getClass().getResource("/icon/action-disabled.png")));
		
		panelAlu.add(goButton);
		goButton.setLocation(102, 243);
		goButton.setContentAreaFilled(false);
		
		goButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				calculate();
			}
		});
		
		pack();
		setVisible(true);

	}
	
	/**
	 * Llama a la calculadora para que realice los c&aacute;lculos
	 */
	private void calculate(){
		float f1 = 0.0f, f2 = 0.0f;
		try{
			f1 = Float.parseFloat(_floatField1.getText());
			f2 = Float.parseFloat(_floatField2.getText());
		} catch (NumberFormatException e){
			JOptionPane.showMessageDialog(this, _noNumbersMessage,"",JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		int operation = 0;
		if(_plusButton.isSelected()){
			operation = Calculator.ADDITION;
		} else if(_minusButton.isSelected()){
			operation = Calculator.SUBSTRACTION;
		} else if(_timesButton.isSelected()){
			operation = Calculator.MULTIPLICATION;
		} else if(_dividedButton.isSelected()){
			operation = Calculator.DIVISION;
		} else {
			JOptionPane.showMessageDialog(this, _noOperationMessage,"",JOptionPane.ERROR_MESSAGE);
			return;
		}
		float res = Calculator.performCalculation(f1,f2,operation);
		_textFieldRes.setText(String.valueOf(res));
		_hexaRes.setText("0x" + Integer.toHexString(Float.floatToRawIntBits(res)));
		
	}
	
}
